<?php

namespace app\admin\service\resource;

use app\admin\model\resource\BoxGamePindaoRelation;
use app\admin\service\BaseService;

/**
 * 频道分类服务类
 */
class BoxGamePindaoRelationService extends BaseService
{
    /**
     * 构造方法
     */
    public function __construct()
    {
        $this->model = new BoxGamePindaoRelation();
    }

    /**
     * Desc : 获取数据列表
     * User : Steven
     * Date : 2024-05-22 18:11
     * @return array
     */
    public function getList(): array
    {
        $limit   = request()->param('limit') ?? 10;
        $id      = request()->param('id');
        $game_id = request()->param('game_id');

        if (empty($id)) return message('参数错误', false);
        $where   = [];
        $where[] = ['a.pindao_id', '=', $id];
        if (!empty($game_id)) $where[] = ['a.game_id', '=', $game_id];

        $result = $this->model->alias('a')
            ->join('cq_box_game b', 'a.game_id = b.id')
            ->field('a.*,b.game_name,b.icon')
            ->where($where)
            ->where('b.mark', '=', 1)
            ->order('a.sort', 'desc')
            ->paginate($limit);
        return [
            'code'  => 0,
            'msg'   => '',
            'count' => $result->total(),
            'data'  => $result->items(),
        ];
    }

    public function status()
    {
        $id = request()->param('id');
        if ($id) {
            $status = request()->param('status');
            $this->model->where('id', $id)->update(['status' => $status]);
            return message('操作成功', true);
        } else {
            return message('参数错误', false);
        }
    }

    public function edit()
    {
        $params = request()->param();
        $id = $params['id'] ?? 0;

        $game_id = $params['game_id'];
        $pindao_id = $params['pindao_id'];

        $result = (new BoxGamePindaoRelation())->where(['game_id'=>$game_id,'pindao_id'=>$pindao_id])->find();

        if(!$id){
            if($result) return message('该频道选择的游戏已存在,不能重复选择',false);
        }else{
           if(!empty($result['id']) && $result['id'] != $id){
               return message('该频道选择的游戏已存在,不能重复选择',false);
           }
        }
        return parent::newEdit($params, false, true);
    }

    public function delete(): array
    {
        $id = request()->param('id');
        if ($id) {
            $this->model->where('id', $id)->delete();
            return message('删除成功', true);
        } else {
            return message('参数错误', false);
        }
    }

}